from odoo import fields, models


class MrpProduction(models.Model):
    _inherit = 'mrp.production'

    def _get_wip_pnl(self):
        self.ensure_one()
        return max(self.workorder_ids.mapped('qty_wip_pnl') + [0])

    def action_cancel(self):
        result = super().action_cancel()

        s_station_order_ids = self.filtered(lambda x: x.state == 'cancel').mapped('s_station_order_ids')
        s_station_order_ids.mapped('task_ids').filtered(lambda x: x.state == 'progress').write({'state': 'cancel'})
        s_station_order_ids.write({'state': 'cancel'})
        return result
